/*
 * This servlet controls the Course overview which contains the data of the course
 * 
 */
package controllers.classes;

import classes.Breadcrumbs;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Course;
import models.Employee;
import dao.CourseDao;
import classes.HibernateUtil;

/**
 *
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class CourseController extends HttpServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        long courseid;
        Course course = new Course();
        List<Employee> teachers = null;

        // checks if Courseid is not null and gets the course
        if (request.getParameter("courseid") != null) {
            courseid = Long.parseLong(request.getParameter("courseid"));
            HibernateUtil hu = new HibernateUtil();
            CourseDao dao = new CourseDao(hu.getSessionFactory().openSession());
            course = dao.get(courseid);
            hu.close();
            request.setAttribute("teachers", teachers);
        } else {
            course.setName("No category selected or the selected category does not exist.");
        }

        request.setAttribute("course", course);

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Category", "Course catalog"),
            new Breadcrumbs("Course?courseid=" + course.getId(), course.getName())
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        // redirect to jsp page
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/course.jsp");
        dispatcher.forward(request, response);



    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
